/*
 * chunk_buf.h
 *
 *  Created on: Mar 29, 2013
 *      Author: root
 */

#ifndef CHUNK_BUF_H_
#define CHUNK_BUF_H_

#include "chunk.h"
#include "smart_lock.h"

/**
 * chunk_buf class
 */
class chunk_buf {
public:
	chunk_buf();
	virtual ~chunk_buf();

	/*
	*chunk_buf init
	*max_chunk_count,
	*chunk_buf_max_size ,max data chunk size
	*/
	int init(uint16_t init_chunk_count,uint24_t chunk_buf_max_size = 0xffff * 2);

	/*
	*chunk  alloc
	*buf_size,get a chunk by chunk buf size
	*/
	int alloc(chunk **pck,size_t buf_size = 0xffff * 2);
	/*
	*chunk  free
	*/
	int free(chunk *pck);

	/*
	*chunk  count
	*/
	int get_current_count(void);
	int get_free_count(void);
private:
	int current_chunk_count;
	ubyte * buf;
	stack<chunk *> free_chunk_pool;
	smart_lock buf_lock;
};

#endif /* CHUNK_BUF_H_ */
